﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhysicalDataProvider;
using DTOFrameworks;
using FooFun.DataElements.Dtos;

namespace FAQFrameworks
{
    public class EventService
    {
        DEDataProvider dataProvider;

        public DEDataProvider DataProvider
        {
            get { return dataProvider; }
            set { dataProvider = value; }
        }

        string[] insertCols
          = new string[] { "EventId", "AccountId", "CreateTime", "IP","Action", 
               "Description","TargetId"};


        string[] selectCols = new string[] { "EventId", "EventNo", "AccountId", "CreateTime", "IP", "Action", "Description", "TargetId" };

        string table = "t_Events";

        string idCol = "EventId";

        public IDataElement CreateEvent(int action, string description, Guid targetId, Guid accountId, string ip)
        {
            Event e = new Event();
            e.EventId = Guid.NewGuid();
            e.Action = action;
            e.Description = description;
            e.AccountId = accountId;
            e.IP = ip;
            e.TargetId = targetId;
            e.CreateTime = DateTime.Now;

            IDataElement de = new ObjectDataElement(e);

            return de;
        }

        public void SaveEvent(IDataElement de)
        {
            EventDbDataElement toDbDE
                = new EventDbDataElement(de);

            DataProvider.Insert(toDbDE, insertCols, table);
        }

        public IDataElement GetElement(IDataElement de)
        {
            IDataElement dbDE = DataProvider.GetScalar(
                DataProviderUtil.GetGuidBase64(de).Value,
                selectCols, table, idCol);

            EventDataElement objDE = new EventDataElement(dbDE);

            return objDE;
        }

        public IDataElement GetElement(Guid guid)
        {
            IDataElement dbDE = DataProvider.GetScalar(
                DataProviderUtil.GetGuidBase64(guid),
                selectCols, table, idCol);

            EventDataElement e = new EventDataElement(dbDE);

            return e;
        }

        class EventDbDataElement : CustomDataElement
        {
            public EventDbDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IDataElement this[string attrName]
            {
                get
                {
                    switch (attrName)
                    {
                        case "EventId":
                            return DataProviderUtil.GetGuidBase64(
                                base[attrName]);
                        case "AccountId":
                            return DataProviderUtil.GetGuidBase64(
                                base[attrName]);
                        case "TargetId":
                            return DataProviderUtil.GetGuidBase64(
                                base[attrName]);
                        case "CreateTime":
                            return DataProviderUtil.GetString(base[attrName]);
                        default:
                            break;
                    }

                    return base[attrName];
                }
                set
                {
                    base[attrName] = value;
                }
            }
        }

        class EventDataElement : CustomDataElement
        {
            public EventDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IDataElement this[string attrName]
            {
                get
                {
                    switch (attrName)
                    {
                        case "EventId":
                            return DataProviderUtil.GetGuid(
                                base[attrName]);
                        case "AccountId":
                            return DataProviderUtil.GetGuid(
                                base[attrName]);
                        case "TargetId":
                            return DataProviderUtil.GetGuid(
                                base[attrName]);
                        default:
                            break;
                    }

                    return base[attrName];
                }
                set
                {
                    base[attrName] = value;
                }
            }
        }
    }
}